package fightplan.actions;

import java.awt.event.ActionEvent;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

import fightplan.Mode;
import fightplan.business.Tournament;
import fightplan.persistence.xml.XmlImport;
import fightplan.views.MainView;

@SuppressWarnings("serial")
public class ImportKyuXmlFightersAction extends UndoableAction {

	private MainView mainView;

	public ImportKyuXmlFightersAction(MainView mainView) {
		super("XML importieren", mainView, "Kategorien aus einem XML-File importieren");
		this.mainView = mainView;
	}

	public boolean doAction(ActionEvent actionEvent) {
		Tournament t = (Tournament) mainView.getTournament().getValue();
		if (t.getMode() != Mode.SKRKYU) {
			JOptionPane.showMessageDialog(mainView,
					"Beim aktuellen Turnier handelt es sich nicht um ein FujiCup Turnier\n"
							+ "XML importiert zur Zeit nur für solche Turniere", "Nicht möglich",
					JOptionPane.ERROR_MESSAGE);
			return false;
		}

		JFileChooser fileChooser = new JFileChooser();
		fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
		fileChooser.setDialogTitle("XML - Files auswählen");
		fileChooser.setMultiSelectionEnabled(true);
		fileChooser.setCurrentDirectory(new File("."));
		fileChooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
			public boolean accept(File f) {
				return !f.isFile() || f.getName().endsWith(".xml");
			}

			public String getDescription() {
				return "*.xml (XML)";
			}
		});
		if (fileChooser.showDialog(mainView, "XML Importieren") == JFileChooser.APPROVE_OPTION) {
			Tournament tournament = (Tournament) mainView.getTournament().getValue();
			return XmlImport.imprtFightersAndTeams(tournament, fileChooser.getSelectedFiles());
		}
		return false;
	}

}